// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/testing/gtest_utils.h>

#include <pollux/substrait/substrait_parser.h>
#include <pollux/substrait/type_utils.h>
#include <pollux/substrait/pollux_to_substrait_type.h>

using namespace kumo::pollux;
using namespace kumo::pollux::substrait;

namespace kumo::pollux::substrait::test {
    class PolluxToSubstraitTypeTest : public ::testing::Test {
    protected:
        void testTypeConversion(const TypePtr &type) {
            SCOPED_TRACE(type->toString());

            google::protobuf::Arena arena;
            auto substraitType = typeConvertor_->toSubstraitType(arena, type);
            auto sameType = substraitParser_->parseType(substraitType);
            ASSERT_TRUE(sameType->kindEquals(type))
        << "Expected: " << type->toString()
        << ", but got: " << sameType->toString();
        }

        std::shared_ptr<PolluxToSubstraitTypeConvertor> typeConvertor_;

        std::shared_ptr<SubstraitParser> substraitParser_ =
                std::make_shared<SubstraitParser>();
    };

    TEST_F(PolluxToSubstraitTypeTest, basic) {
        testTypeConversion(BOOLEAN());

        testTypeConversion(TINYINT());
        testTypeConversion(SMALLINT());
        testTypeConversion(INTEGER());
        testTypeConversion(BIGINT());

        testTypeConversion(REAL());
        testTypeConversion(DOUBLE());

        testTypeConversion(VARCHAR());
        testTypeConversion(VARBINARY());

        testTypeConversion(ARRAY(BIGINT()));
        testTypeConversion(MAP(BIGINT(), DOUBLE()));

        testTypeConversion(ROW({"a", "b", "c"}, {BIGINT(), BOOLEAN(), VARCHAR()}));
        testTypeConversion(
            ROW({"a", "b", "c"},
                {BIGINT(), ROW({"x", "y"}, {BOOLEAN(), VARCHAR()}), REAL()}));
        testTypeConversion(ROW({}, {}));
    }
} // namespace kumo::pollux::substrait::test
